package com.hengpeng.itfin.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Payment
{
	private static Log logger = LogFactory.getLog(Payment.class);

	private static final String datePattern = "yyyyMMddHHmmss";

	private static final SimpleDateFormat df = new SimpleDateFormat(datePattern);

	public static String getCurrDate()
	{
		Date today = new Date();
		String todayAsString = df.format(today);
		return todayAsString;
	}

	/**
	 * 提款文件上传生成签名包
	 * @param paygateway
	 * @param service
	 * @param partner
	 * @param biz_type
	 * @param file_digest_type
	 * @param sign_type
	 * @param key
	 * @param input_charset
	 * @param digest_bptb_pay_file
	 * @return
	 */
	public static String CreateUrlForUpload(String service, String partner, String biz_type, String file_digest_type,
			String sign_type, String key, String input_charset, String digest_bptb_pay_file)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("biz_type", biz_type);
		params.put("file_digest_type", file_digest_type);
		params.put("_input_charset", input_charset);
		params.put("digest_bptb_pay_file", digest_bptb_pay_file);

		String sign = Md5Encrypt.md5(getContent(params, key));
		return sign;

	}

	public static String CreateUrlForUpload(String service, String partner, String biz_type, String file_digest_type,
			String sign_type, String key, String input_charset, String digest_bptb_pay_file, String bussiness_type)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("biz_type", biz_type);
		params.put("file_digest_type", file_digest_type);
		params.put("_input_charset", input_charset);
		params.put("digest_bptb_pay_file", digest_bptb_pay_file);
		params.put("bussiness_type", bussiness_type);

		String sign = Md5Encrypt.md5(getContent(params, key));
		return sign;

	}

	public static String CreateUrlForDown(String service, String partner, String biz_type, String file_name,
			String sign_type, String key, String input_charset)
	{

		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("biz_type", biz_type);
		params.put("file_name", file_name);
		params.put("_input_charset", input_charset);

		String sign = Md5Encrypt.md5(getContent(params, key));
		return sign;

	}

	public static String CreateUrl2(String service, String partner, String biz_type, String date, String sign_type,
			String key, String input_charset)
	{
		Map<String, String> params = new HashMap<String, String>();
		params.put("service", service);
		params.put("partner", partner);
		params.put("biz_type", biz_type);
		params.put("date", date);
		params.put("_input_charset", input_charset);

		String sign = Md5Encrypt.md5(getContent(params, key));

		return sign;

	}

	private static String getContent(Map<String, String> params, String privateKey)
	{
		List<String> keys = new ArrayList<String>(params.keySet());
		Collections.sort(keys);
		String prestr = "";
		for (int i = 0; i < keys.size(); i++)
		{
			String key = (String) keys.get(i);
			String value = (String) params.get(key);
			if (i == keys.size() - 1)
			{
				prestr = prestr + key + "=" + value;
			}
			else
			{
				prestr = prestr + key + "=" + value + "&";
			}
		}
		logger.info("MD5签名原串：" + prestr + privateKey);
		return prestr + privateKey;
	}

	/**
	 * <pre>对文件进行md5加密</pre>
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public static String md5File(File file)
	{
		String str = "";
		FileInputStream fileInputStream = null;
		try
		{
			fileInputStream = new FileInputStream(file);
			int size = fileInputStream.available();
			byte[] content = new byte[size];
			// md5方式*********************************************************************
			DigestUtils.md5(content);
			// 需要使用IOUtils.toByteArray是否提醒商户知道?
			str = DigestUtils.md5Hex(IOUtils.toByteArray(fileInputStream));
		}
		catch (Exception e)
		{
			logger.error("对付款文件加密失败:", e);
		}
		finally
		{
			try
			{
				if (fileInputStream != null)
				{
					fileInputStream.close();
				}
			}
			catch (IOException e)
			{
				logger.error("关闭文件流异常", e);
			}
		}
		return str;
	}
}
